package com.it.studentsystem;

import java.util.Scanner;
import java.util.ArrayList;

public class My_StudentSystem {
    public static void main(String[] args) {
        ArrayList<My_Student>  list = new ArrayList<>();
        while (true) {
            System.out.println("-----欢迎来到学生成长管理系统-----");
            System.out.println("1:添加学生");
            System.out.println("2:删除学生");
            System.out.println("3:修改学生");
            System.out.println("4:查询学生");
            System.out.println("5:退出");
            System.out.println("请输入您的选择:");
            Scanner sc = new Scanner(System.in);
            int choice = sc.nextInt();
            switch(choice){
                case 1 -> addStudent(list);
                case 2 -> deleteStudent(list);
                case 3 -> updateStudent(list);
                case 4 -> findStudent(list);
                case 5 -> {
                    System.out.println("退出");
                    System.exit(0);
                }
                default -> System.out.println("没有这个选项");
            }
        }
    }
    //添加学生
    public static void addStudent(ArrayList<My_Student> list){
        //System.out.println("添加学生");
        My_Student s = new My_Student();

        Scanner sc = new Scanner(System.in);
        String id = null;
        while (true) {
            System.out.println("请输入学生的id：");
            id = sc.next();
            boolean flag = contains(list,id);
            if(flag){
                System.out.println("该学生id已存在，请重新输入");
            }else{
                s.setId(id);
                break;
            }
        }
        System.out.println("请输入学生的Name：");
        String Name = sc.next();
        s.setName(Name);

        System.out.println("请输入学生的Age：");
        int Age = sc.nextInt();
        s.setAge(Age);

        System.out.println("请输入学生的address：");
        String Address = sc.next();
        s.setAddress(Address);

        //把学生对象添加到集合当中
        list.add(s);
        System.out.println("添加学生信息成功");
    }

    //删除学生
    public static void deleteStudent(ArrayList<My_Student> list){
        //System.out.println("删除学生");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除的学生id");
        String id = sc.next();
        int idIndex = getIndex(list,id);
        if(idIndex >= 0){
            list.remove(idIndex);
            System.out.println("删除成功");
        }else{
            System.out.println("不存在该学生id");
        }
    }

    //修改学生
    public static void updateStudent(ArrayList<My_Student> list){
        //System.out.println("修改学生");
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改的学生id");
        String id = sc.next();
        int idIndex = getIndex(list,id);
        if(idIndex == -1){
            System.out.println("该学生id不存在");
            return;
        }
        //获取要修改的学生对象
        My_Student s = list.get(idIndex);

        System.out.println("请输入要修改学生的姓名：");
        String Name = sc.next();
        s.setName(Name);

        System.out.println("请输入要修改的学生年龄：");
        int Age = sc.nextInt();
        s.setAge(Age);

        System.out.println("请输入要修改的学生地址：");
        String Address = sc.next();
        s.setAddress(Address);

        System.out.println("修改成功");
    }

    //查询学生
    public static void findStudent(ArrayList<My_Student> list){
        //System.out.println("查询学生");
        //没有学生对象的判断
        if(list.size() == 0){
            System.out.println("没有学生对象");
            return;
        }
        //有学生对象，则遍历集合，打印全部学生对象
        //打印表头
        System.out.println("id\t\t姓名\t年龄\t家庭住址");
        for (int i = 0; i < list.size(); i++) {
            My_Student stu = list.get(i);
            System.out.println(stu.getId()+"\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getAddress());
        }
    }

    //判断id在集合中是否唯一
    public static boolean contains(ArrayList<My_Student> list, String id){
        for (int i = 0; i < list.size(); i++) {
            My_Student stu = list.get(i);
            if(id.equals(stu.getId())){
                return true;
            }
        }
        return false;
    }

    //查询学生id是否存在
    public static int getIndex(ArrayList<My_Student> list, String id){
        for (int i = 0; i < list.size(); i++) {
            My_Student stu = list.get(i);
            String sid = stu.getId();
            if(id.equals(sid)){
                return i;
            }
        }
        return -1;
    }
}
